﻿//========================================================= 
/**@file StreamFollower.h
 * @brief 通过输出流输出日志
 * 
 * @date 2018-06-18   11:35:19
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_STREAMFOLLOWER_H_
#define _LIBZHOUYB_STREAMFOLLOWER_H_
//--------------------------------------------------------- 
#include "../ILogCat.h"

#include <sstream>
using std::ostringstream;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace extension {
namespace logcat {
//--------------------------------------------------------- 
/// 使用 << 流输出日志的方式
class StreamFollower : public LogCatFollower
{
public:
    typedef basic_ostream<char, char_traits<char> > char_ostream;
    typedef char_ostream & (*stream_func)(char_ostream&);
protected:
    ostringstream _stream;

    void _copy(StreamFollower& obj)
    {
        LogCatFollower::_copy(obj);
        _stream << obj._stream.str();
    }
public:
    LogCatFollowerConstructor(StreamFollower)

    virtual ~StreamFollower()
    {
        Append(_stream.str());
    }

    template<class T>
    StreamFollower& operator <<(const T& val)
    {
        if(IsEnable())
        {
            _stream << val;
        }
        return (*this);
    }
    StreamFollower& operator <<(bool b)
    {
        if(IsEnable())
        {
            _stream << (b ? "true" : "false");
        }
        return (*this);
    }
    StreamFollower& operator <<(stream_func func)
    {
        if(IsEnable())
        {
            _stream << func;
        }
        return (*this);
    }
};
//--------------------------------------------------------- 
} // namespace logcat
} // namespace extension
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_STREAMFOLLOWER_H_
//========================================================= 